// pages/my/stockRecord/stockRecord.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    cover:'active',
    currentIndex:1,
    current:0,
    tabs:['我的存酒','取酒记录','过期酒水'],
    urls:[],
    access:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    let urls = _this.data.urls;
    wx.request({
      url: app.globalData.baseUrl + '/api/house/house_num',
      data: {
        session_key: app.gs()
      },
      success(res) {
        let houNum;
        let houses = res.data.data;
        // let num = res.data.data
        if (houses.num.is_scan == 0) {
          let index = 0;
          let hou = [];
          for (let i = 0; i < houses.house.length; i++) {
            hou[i] = houses.house[i].name
          }
          hou.unshift('选择房号');

         
          _this.setData({
            houses: res.data.data,
            hou: hou,
            num: res.data.data.num,
            houseIndex: '选择房号'
          })
        } else if (houses.num.is_scan == 1) {
          let index = 0;
          let hou = [];
          for (let i = 0; i < houses.house.length; i++) {
            hou[i] = houses.house[i].name
          }
          hou.unshift('选择房号');
          console.log(hou[0])
          if (houses.num.house == 'undefined') {
            houNum = hou[0]
          } else {
            houNum = houses.num.house
          }
          console.log()
          _this.setData({
            houses: res.data.data,
            hou: hou,
            num: res.data.data.num,
            houseIndex: res.data.data.num.house,
            houNum: houNum,
          })
        }
      }
    })
    let count = [];
    wx.request({
      url: app.globalData.baseUrl + '/api/drinks/access_info',
      data: {
        session_key: app.gs()
      },
      success: function (res) {
        console.log(res)
        let access_id;
        if (res.data.data.access == null) {
          res.data.data.access = []
        } else if (res.data.data.access.length == 0) {
           access_id = ''
        } else if (!res.data.data.access.length == 0) {
          for (let i = 0; i < res.data.data.access.length; i++) {
            urls[i] = '../../../imgs/noCheck.png',
              count[i] = res.data.data.access[i].num
          }
          urls[0] = '../../../imgs/checked.png';
           access_id = res.data.data.access[0].id
        }

        if (res.data.data.access_record.out == null) {
          res.data.data.access_record.out = []
        }
        if (res.data.data.access_record.outs == null) {
          res.data.data.access_record.outs = []
        }
        if (res.data.data.access_past == null) {
          res.data.data.access_past = []
        }
        let access = _this.data.access;
        let acc = { access_id: access_id, num: count[0] }
        access.push(acc)
        _this.setData({
          record: res.data.data,
          urls: urls,
          count: count,
          access:access
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  tabs(e){
    this.setData({
      current:e.currentTarget.dataset.index
    })
  },
  titleCon(e){
    this.setData({
      currentIndex:e.currentTarget.dataset.ind,
      
    })
  },
  chooseWine(e){
    console.log(e)
    let _this = this;
    let urls = this.data.urls;
    let inde = e.currentTarget.dataset.inde;
    let coun = e.currentTarget.dataset.coun;
    let access = _this.data.access;
    let acc;
    if (urls[inde] =='../../../imgs/noCheck.png'){
      urls[inde] = '../../../imgs/checked.png';
      acc = {access_id: e.currentTarget.id ,num: coun}
      access.push(acc);
    }else{
      urls[inde] = '../../../imgs/noCheck.png';
      access.splice(inde,1);
      
    }

    this.setData({
      urls: urls,
      // coun:coun,
      // access_id:e.currentTarget.id
      access:access
    })
  },

  hideChoose(e){
    this.setData({
      cover:'active'
    })
  },
  showChoose(){
    this.setData({
      cover:''
    })
  },
  reduceAdd(e){
    let _this = this;
    let num = Math.floor(e.currentTarget.dataset.num);
    let urls = this.data.urls;
    let index = e.currentTarget.dataset.index;
    let coun = Math.floor(e.currentTarget.dataset.coun);
    let access = [];
    let count = _this.data.count;
    let acce,acc = [];

    if(e.currentTarget.id=='add') {
      console.log(typeof(coun))
      console.log(typeof (coun))
      if(coun <num){
        coun+=1
      }else {
        coun = coun
      }
      urls[index] = '../../../imgs/checked.png';
    }else {
      if(coun>1){
        coun-=1
      }else {
        coun = coun
      }
      urls[index] = '../../../imgs/checked.png';

    }

    count[index] = (coun*1).toFixed(2);
    
    for (let i = 0; i < urls.length;i++){
      if (urls[i] == '../../../imgs/checked.png'){
        acc = { access_id: _this.data.record.access[i].id, num: count[i] }
        access.push(acc);
      }
    }
    _this.setData({
      count:count,
      urls:urls,
      access:access,
      coun: coun
    })
  },
  takeWine(e){
    let _this = this;
    if (_this.data.houseIndex =='选择房号'){
      wx.showToast({
        title: '选择房号',
      })
      return
    }
    let access = JSON.stringify(_this.data.access);
    wx.request({
      // url: app.globalData.baseUrl +'/api/drinks/get_access',
      url: app.globalData.baseUrl + '/api/drinks/get_access_list',
      data:{
        session_key:app.gs(),
        access: access,
        house_num: _this.data.houNum
      },
      method:'POST',
      header:{'content-type':'application/x-www-form-urlencoded'},
      success:function(res){

        if(res.data.code==200){
          setTimeout(function(){

            wx.showToast({
              title: '取酒成功',
            })
           setTimeout(function(){
            wx.switchTab({
              url: '../../index/index',
            })
           },1000)
          },1000)
        }

        
      }
    })
  },
  bindPickerChange: function (e) {
    let _this = this;
    let index = e.detail.value;
    console.log(index)
    let ind = parseInt(index) - 1;
    let houseId, house_num;
    let houseIndex = _this.data.houseIndex;
    if (ind == -1) {
        houseId = -1,
        house_num = _this.data.hou[0]
        houseIndex = _this.data.hou[0]
    } else {
      houseId = _this.data.houses.house[ind].id;
      house_num = _this.data.houses.house[ind].name;
      houseIndex = _this.data.houses.house[ind].name
    }
    _this.setData({
      index: index,
      houseId: houseId,
      house_num: house_num,
      houseIndex: houseIndex,
      houNum: house_num
    })
  },

  saoma(e) {
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        let scan = res.path;
        let path = scan.split('?')[0];
        //      分配房号-index
        if (path == 'pages/index/index') {
          let house_num = scan.match(/scene=(\S*)/)[1];

          wx.request({
            url: app.globalData.baseUrl + '/api/house/band',

            data: {
              session_key: app.gs(),
              house_num: house_num
            },
            success(res) {
              if (res.data.message == 200) {
                wx.showToast({
                  title: '确认完成'
                })

                let urls = _this.data.urls;
                wx.request({
                  url: app.globalData.baseUrl + '/api/house/house_num',
                  data: {
                    session_key: app.gs()
                  },
                  success(res) {
                    console.log('house-res', res)
                    let houNum;
                    let houses = res.data.data;
                    if (houses.num.is_scan == 0) {
                      let index = 0;
                      let hou = [];
                      for (let i = 0; i < houses.house.length; i++) {
                        hou[i] = houses.house[i].name
                      }
                      hou.unshift('选择房号');


                      _this.setData({
                        houses: res.data.data,
                        hou: hou,
                        num: res.data.data.num,
                        houseIndex: '选择房号'
                      })
                    } else if (houses.num.is_scan == 1) {
                      let index = 0;
                      let hou = [];
                      for (let i = 0; i < houses.house.length; i++) {
                        hou[i] = houses.house[i].name
                      }
                      hou.unshift('选择房号');
                      console.log(hou[0])
                      if (houses.num.house == 'undefined') {
                        houNum = hou[0]
                      } else {
                        houNum = houses.num.house
                      }
                      _this.setData({
                        houses: res.data.data,
                        hou: hou,
                        num: res.data.data.num,
                        houseIndex: res.data.data.num.house,
                        houNum: houNum,
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  }
})